package com.caregiver.watch.business.user.service.impl;

import com.caregiver.watch.business.user.dao.UserAddressDao;
import com.caregiver.watch.business.user.model.dto.BindAddressParam;
import com.caregiver.watch.business.user.model.dto.EditAddressParam;
import com.caregiver.watch.business.user.model.po.UserAddress;
import com.caregiver.watch.business.user.model.vo.DeliveryAddressVo;
import com.caregiver.watch.business.user.service.UserAddressService;
import com.caregiver.watch.business.user.utils.UserInfoUtils;
import com.caregiver.watch.common.exception.business.BusinessException;
import com.caregiver.watch.common.utils.collection.ListUtils;
import com.caregiver.watch.common.utils.md5.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.caregiver.watch.common.constant.Constants.DASH;
import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 用户地址服务
 * @Author 疆戟
 * @Date 2025/8/17 17:44
 * @Version 1.0
 */
@Service
@Slf4j
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressDao userAddressDao;

    @Override
    public Boolean bindAddress(BindAddressParam param) {
        // 验证参数不能为空
        param.validate();
        UserAddress po = param.toPo();
        po.setUserId(UserInfoUtils.getUserId());
        po.setMd5(buildAddressMd5(po));
        // 判断地址是否已经存在
        if (userAddressDao.checkAddressExists(po.getUserId(), po.getMd5())) {
            throw new BusinessException("地址已存在");
        }
        // 如果是默认地址 则将之前默认地址改为非默认
        if (BooleanUtils.isTrue(param.getIsDefault())) {
            userAddressDao.updateAllAddressToNotDefault(po.getUserId());
        }
        // 如果是默认地址 则将之前默认地址改为非默认
        return userAddressDao.insertUserAddress(po) > ZERO;
    }


    /**
     * 构建地址md5
     *
     * @param po
     * @return
     */
    private String buildAddressMd5(UserAddress po) {
        if (Objects.isNull(po)) {
            return StringUtils.EMPTY;
        }
        return MD5Utils.encode(Stream.of(po.getConsignee(), po.getPhone(), po.getProvince(), po.getCity(), po.getDistrict(), po.getDetailAddress()).collect(Collectors.joining(DASH)));
    }

    @Override
    public List<DeliveryAddressVo> queryAddress() {
        Long userId = UserInfoUtils.getUserId();
        List<UserAddress> userAddresses = userAddressDao.selectUserAddressListByUserId(userId);
        return ListUtils.map(userAddresses, DeliveryAddressVo::build);
    }

    @Override
    public Boolean deleteAddress(Long addressId) {
        if (Objects.isNull(addressId)) {
            return Boolean.FALSE;
        }
        Long userId = UserInfoUtils.getUserId();
        return userAddressDao.deleteUserAddressById(userId, addressId) > ZERO;
    }

    @Override
    public Boolean editAddress(EditAddressParam param) {
        param.validate();
        UserAddress po = param.toPo();
        po.setUserId(UserInfoUtils.getUserId());
        po.setMd5(buildAddressMd5(po));
        // 判断地址是否已经存在
        if (userAddressDao.checkAddressExists(po.getUserId(), po.getMd5())) {
            throw new BusinessException("地址已存在");
        }
        // 跟新地址
        boolean success = userAddressDao.updateUserAddress(po) > ZERO;
        // 如果是默认地址 则更新默认地址
        if (success && BooleanUtils.isTrue(param.getIsDefault())) {
            userAddressDao.updateDefaultAddress(po.getUserId(), po.getId());
        }
        return success;
    }
}
